package org.xa3apm.vote.bean.db;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.xa3apm.vote.hibernate.entities.Address;
import org.xa3apm.vote.hibernate.entities.Candidate;
import org.xa3apm.vote.hibernate.entities.Party;
import org.xa3apm.vote.hibernate.entities.Person;
import org.xa3apm.vote.hibernate.entities.Settlement;

@Stateless
public class InitPerson {
	@PersistenceContext(unitName = "vote")
	EntityManager em;

	public void initPersons() {
		Person p1 = new Person();
		p1.setId(352312332l);
		p1.setfName("Кирил");
		p1.setmName("Трифонов");
		p1.setlName("Петров");
		p1.setBirthDay(parseDate("1975-3-15"));
		Address p1Address = new Address();
		p1Address.setNumber(32);
		p1Address.setPerson(p1);
		p1Address.setSettlements((Settlement) em
				.createNamedQuery("Settlement.getByName")
				.setParameter("name", "Несебър").getSingleResult());
		p1Address.setStreet("Митрополитска");
		p1.setAddress(p1Address);
		Person p2 = new Person();
		p2.setId(35287332l);
		p2.setfName("Иван");
		p2.setmName("Стефанов");
		p2.setlName("Киров");
		p2.setBirthDay(parseDate("1945-10-22"));
		Address p2Address = new Address();
		p2Address.setNumber(14);
		p2Address.setPerson(p2);
		p2Address.setSettlements((Settlement) em
				.createNamedQuery("Settlement.getByName")
				.setParameter("name", "Несебър").getSingleResult());
		p2Address.setStreet("св.св. Кирил и Методий");
		p2.setAddress(p2Address);
		Person p3 = new Person();
		p3.setId(352342086l);
		p3.setfName("Петър");
		p3.setmName("Иванов");
		p3.setlName("Стамов");
		p3.setBirthDay(parseDate("1956-06-15"));
		Address p3Address = new Address();
		p3Address.setComplex("Младост");
		p3Address.setNumber(5);
		p3Address.setPerson(p3);
		p3Address.setSettlements((Settlement) em
				.createNamedQuery("Settlement.getByName")
				.setParameter("name", "Равда").getSingleResult());
		p3Address.setStreet("Иван Вазов");
		p3.setAddress(p3Address);
		Person p4 = new Person();
		p4.setId(65345345l);
		p4.setfName("Ставри");
		p4.setmName("Попов");
		p4.setlName("Митев");
		p4.setBirthDay(parseDate("1982-10-25"));
		Address p4Address = new Address();
		p4Address.setNumber(12);
		p4Address.setPerson(p4);
		p4Address.setSettlements((Settlement) em
				.createNamedQuery("Settlement.getByName")
				.setParameter("name", "Свети Влас").getSingleResult());
		p4Address.setStreet("Братя Миладинови");
		p4.setAddress(p4Address);
		Person p5 = new Person();
		p5.setId(352312392l);
		p5.setfName("Иванка");
		p5.setmName("Михова");
		p5.setlName("Димова");
		p5.setBirthDay(parseDate("1975-3-11"));
		Address p5Address = new Address();
		p5Address.setNumber(22);
		p5Address.setPerson(p5);
		p5Address.setSettlements((Settlement) em
				.createNamedQuery("Settlement.getByName")
				.setParameter("name", "Равда").getSingleResult());
		p5Address.setStreet("Хан Аспарух");
		p5.setAddress(p5Address);
		create(p1, p1Address, p2, p2Address, p3, p3Address, p4, p4Address, p5,
				p5Address);
		// Create Candidates;
		Party p = new Party();
		p.setName("Зелена");
		Candidate c1 = new Candidate();
		c1.setId(352382392l);
		c1.setfName("Красимир");
		c1.setmName("Колев");
		c1.setlName("Пеев");
		c1.setBirthDay(parseDate("1972-10-15"));
		c1.setParty(p);
		p.getCandidate().add(c1);
		Address c1Address = new Address();
		c1Address.setNumber(7);
		c1Address.setPerson(c1);
		c1Address.setSettlements((Settlement) em
				.createNamedQuery("Settlement.getByName")
				.setParameter("name", "Несебър").getSingleResult());
		c1Address.setStreet("Крайбрежна");
		c1.setAddress(c1Address);
		Candidate c2 = new Candidate();
		c2.setId(39257330l);
		c2.setfName("Стефан");
		c2.setmName("Павлов");
		c2.setlName("Киров");
		c2.setBirthDay(parseDate("1949-07-16"));
		Address c2Address = new Address();
		c2Address.setNumber(4);
		c2Address.setPerson(c2);
		c2Address.setSettlements((Settlement) em
				.createNamedQuery("Settlement.getByName")
				.setParameter("name", "Несебър").getSingleResult());
		c2Address.setStreet("св.св. Кирил и Методий");
		c2.setAddress(c2Address);
		Candidate c3 = new Candidate();
		c3.setId(252342086l);
		c3.setfName("Павел");
		c3.setmName("Иванов");
		c3.setlName("Станчев");
		c3.setBirthDay(parseDate("1969-08-13"));
		Address c3Address = new Address();
		c3Address.setComplex("Младост");
		c3Address.setNumber(21);
		c3Address.setPerson(c3);
		c3Address.setSettlements((Settlement) em
				.createNamedQuery("Settlement.getByName")
				.setParameter("name", "Равда").getSingleResult());
		c3Address.setStreet("Иван Вазов");
		c3.setAddress(c3Address);
		Candidate c4 = new Candidate();
		c4.setId(65393475l);
		c4.setfName("Георги");
		c4.setmName("Христов");
		c4.setlName("Христов");
		c4.setBirthDay(parseDate("1982-03-15"));
		Address c4Address = new Address();
		c4Address.setNumber(5);
		c4Address.setPerson(c4);
		c4Address.setSettlements((Settlement) em
				.createNamedQuery("Settlement.getByName")
				.setParameter("name", "Свети Влас").getSingleResult());
		c4Address.setStreet("Любен Каравелов");
		c4.setAddress(c4Address);
		Candidate c5 = new Candidate();
		c5.setId(359392392l);
		c5.setfName("Станка");
		c5.setmName("Попова");
		c5.setlName("Кирова");
		c5.setBirthDay(parseDate("1978-08-03"));
		Address c5Address = new Address();
		c5Address.setNumber(32);
		c5Address.setPerson(c5);
		c5Address.setSettlements((Settlement) em
				.createNamedQuery("Settlement.getByName")
				.setParameter("name", "Равда").getSingleResult());
		c5Address.setStreet("Странджа");
		c5.setAddress(c5Address);
		create(p, c1, c1Address, c2, c2Address, c3, c3Address, c4, c4Address,
				c5, c5Address);

		// Candidate c2 = (Candidate)
		// em.createNamedQuery("Person.getById").setParameter("id",
		// 352312392l).getSingleResult();
		// c2.setType(VoteType.MAYOR);
		// em.merge(c2);

	}

	public InitPerson() {

	}

	public Date parseDate(String sDate) {
		Date date = null;
		try {
			date = DateFormat.getDateInstance().parse(sDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}

	public <T> void create(T... input) {
		for (T t : input) {
			em.persist(t);
		}
	}
}
